﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Threading;
using System.Net;
using System.Net.Mail;
using RookieLibSharp.Configuration;
namespace BirthdayRemainder
{
    public partial class FrmGiveAdvice : Form
    {
#region 全局变量
        /// <summary>
        /// 邮件body初始值
        /// </summary>
        string STR_MAIL_BODY = "";//\n本人长期承接Excel自动化，定制加载项，网页数据采集等工作\n\n如需帮助，请留下您的联系方式，如邮箱或者QQ等，如需详细交流请加QQ（771494925）";
        /// <summary>
        /// 邮件主题
        /// </summary>
        string m_MailTheme = "";
        /// <summary>
        /// 邮件正文
        /// </summary>
        string m_MailBody = "";
        /// <summary>
        /// 附加邮件地址
        /// </summary>
        string m_CustomMailAddr = "";

#endregion

#region 委托事件
        /// <summary>
        /// 进度条显示隐藏委托
        /// </summary>
        /// <param name="_Visible"></param>
        private delegate void dg_SetProgressVisible(bool _Visible);
        /// <summary>
        /// 进度条值设置委托
        /// </summary>
        /// <param name="_Val"></param>
        private delegate void dg_SetProgressValue(int _Val);

        private delegate void dg_CloseFrm();
        /// <summary>
        /// 进度条显示隐藏
        /// </summary>
        /// <param name="_Visible"></param>
        private void SetProgressVisible(bool _Visible)
        {
            if (this.progressBar.InvokeRequired)
            {
                dg_SetProgressVisible method = SetProgressVisible;
                this.progressBar.Invoke(method, _Visible);
                return;
            }
            this.progressBar.Visible = _Visible;
        }
        /// <summary>
        /// 进度条值设置
        /// </summary>
        /// <param name="_Val"></param>
        private void SetProgressValue(int _Val)
        {
            if (this.progressBar.InvokeRequired)
            {
                dg_SetProgressValue method = SetProgressValue;
                this.progressBar.Invoke(method, _Val);
                return;
            }
            this.progressBar.Value = _Val;
        }
        private void CloseFrm()
        {
            if (this.InvokeRequired)
            {
                dg_CloseFrm method = CloseFrm;
                this.Invoke(method);
                return;
            }
            this.Close();
        }
        private delegate void dg_SetBtnEnable(bool bEnabled);
        private void SetBtnEnable(bool bEnabled)
        {
            if (this.btnSendMail.InvokeRequired)
            {
                dg_SetBtnEnable method = SetBtnEnable;
                this.btnSendMail.Invoke(method, bEnabled);
            }
            else
            {
                this.btnSendMail.Enabled = bEnabled;
            }
        }
#endregion
        public FrmGiveAdvice()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗体加载时响应过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmGiveAdvice_Load(object sender, EventArgs e)
        {
            this.cbMailTheme.Items.Clear();
            this.cbMailTheme.Items.Add("此软件有问题，我要提交Bug");
            this.cbMailTheme.Items.Add("此软件不够完善，我要提建议");
            this.cbMailTheme.SelectedIndex = 1;
            this.rtbmMailBody.Text = STR_MAIL_BODY;
            this.progressBar.Visible = false;
            LoadCfg();
        }
        /// <summary>
        /// 发送邮件按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendMail_Click(object sender, EventArgs e)
        {

            m_MailTheme = this.cbMailTheme.Text;
            m_CustomMailAddr = this.tbMailAddr.Text;
            m_MailBody = this.rtbmMailBody.Text + "\n" + m_CustomMailAddr;
            if (false == CheckValidaty())
            {
                return;
            }
            Thread tSendMail = new Thread(SendMail);
            tSendMail.Start();
            SaveCfg();
        }
        /// <summary>
        /// 有效性检测
        /// </summary>
        /// <param name="_MailAddr"></param>
        /// <returns></returns>
        private bool CheckValidaty()
        {
            if (false == Funcs.CheckNetConnection())
            {
                Msg("网络连接不成功，请检查网络连接，谢谢！");
                return false;
            }
            if (false == Funcs.CheckMailAddr(m_CustomMailAddr))
            {
                Msg("请正确填写您的联系方式，谢谢！");
                return false;
            }
            
            if (this.rtbmMailBody.Text.Trim().Length == 0)
            {
                Msg("请填写邮件内容，谢谢！");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        private void SaveCfg()
        {
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_ADVICE_CUSTOMER_MAIL, this.tbMailAddr.Text);
        }
        /// <summary>
        /// 加载设置
        /// </summary>
        private void LoadCfg()
        {
            this.tbMailAddr.Text = Cfg.ReadString(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_ADVICE_CUSTOMER_MAIL);
        }
        /// <summary>
        /// 主题改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbMailTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_MailBody = this.rtbmMailBody.Text;
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        private void SendMail()
        {
            SetProgressValue(0);
            SetProgressVisible(true);
            SetBtnEnable(false);
            MailMessage MailMsg = new MailMessage();
            SetProgressValue(20);
            MailMsg.From = new MailAddress("raoqiang1988@126.com", "[BirthdayRemainder]");
            MailMsg.To.Add(new MailAddress("raoqiangnihao@126.com"));
            MailMsg.Subject = m_MailTheme;//邮件主题
            MailMsg.IsBodyHtml = true;
            MailMsg.BodyEncoding = Encoding.UTF8;
            MailMsg.Body = m_MailBody;//邮件正文
            MailMsg.Priority = MailPriority.Normal;
            SmtpClient MailClient = new SmtpClient("smtp.126.com", 25);
            SetProgressValue(50);
            MailClient.Credentials = new NetworkCredential("raoqiang1988@126.com", "raoqiang12345");
            MailClient.EnableSsl = true;
            SetProgressValue(70);
            bool bSucess = false;
            try
            {
                MailClient.Send(MailMsg);
                SetProgressValue(100);
                Msg("发送成功");
                bSucess = true;
            }
            catch (System.Exception ex)
            {
                Msg("发送失败，请重新发送!\n" + ex.Message);
            }
            MailMsg = null;
            MailClient = null;
            SetProgressVisible(false);
            SetBtnEnable(true);
            if (bSucess)
            {
                CloseFrm();
            }
        }
        private void FrmGiveAdvice_FormClosed(object sender, FormClosedEventArgs e)
        {
            SaveCfg();
        }
        private delegate DialogResult dl_Msg(string str, MessageBoxButtons btn);
        /// <summary>
        /// 消息显示
        /// </summary>
        /// <param name="str">消息内容</param>
        /// <param name="btn">按钮类型</param>
        /// <returns>消息窗体结果</returns>
        private DialogResult Msg(string str, MessageBoxButtons btn = MessageBoxButtons.OK)
        {
            if (this.InvokeRequired)
            {
                dl_Msg method = Msg;
                return (DialogResult)Invoke(method, str, btn);
            }
            return MessageBox.Show(str, "提醒", btn);
        }
    }
}
